<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <div th:replace="m/head :: head"></div>

    <title>优惠券</title>
</head>

<body class="body-gray">

<div th:replace="m/head :: body('banner.hidden', 'head-no-menu', '转赠优惠券')"></div>


<!--主体内容-->
<div class="myCoupon-content myCoupon-give">
    <input type="hidden" id="id" th:value="${id}">
    <h3>请输入赠送人手机号</h3>
    <input class="phone" type="text" id="target" placeholder="输入手机号"/>
    <p class="tishi"></p>
    <div class="myCoupon-btn">
        <a href="javascript:void(0)" id="present-btn">确定赠送</a>
        <a href="javascript: window.history.back(-1);">返回</a>
    </div>
</div>

<div th:replace="m/foot :: foot"></div>

</body>

<div th:replace="m/foot :: footer('')"></div>

<script>
    $(function () {
        $('#present-btn').click(function () {
            if (!(/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/.test($('#target').val()))) {
                $('.tishi').text('手机号格式错误');
                return false;
            }

            $('.tishi').text('');

            var id = $('#id').val();
            var target = $('#target').val();
            $.ajax({
                type: 'post',
                url: '/personal/discountcoupon/api/present',
                data: {
                    id: id,
                    target: target,
                },
                beforeSend: function (xhr) {
                    xhr.setRequestHeader($("meta[name='_csrf_header']").attr("content"), $("meta[name='_csrf']").attr("content"));
                },
                dataType: 'json',
                success: function (result) {
                    if (result.data == true) {
                        alert(Messages.SUCCESS);
                        window.history.go(-1);
                    } else {
                        $('.tishi').text(result.error || Messages.PRESENT_FAILED);
                    }
                },
                error: function (e) {
                    $('.tishi').text(Messages.ERROR);
                }
            })
        })
    })
</script>

</html>
